package org.gdstash.ui.util;

import javax.swing.*;
import javax.swing.plaf.basic.BasicComboBoxRenderer;
import java.awt.*;

public class EnabledComboBoxRenderer<E> extends BasicComboBoxRenderer {
    private EnabledComboBoxModel model;
    private Color disabledColor;

    public EnabledComboBoxRenderer(final EnabledComboBoxModel model) {
        this.model = model;
        this.disabledColor = Color.lightGray;
    }

    public void setDisabledColor(final Color disabledColor) {
        this.disabledColor = disabledColor;
    }

    @Override
    public Component getListCellRendererComponent(final JList list, final Object value, final int index, final boolean isSelected, final boolean cellHasFocus) {
        final Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        if (this.model == null) {
            c.setBackground(super.getBackground());
            c.setForeground(super.getForeground());
            return c;
        }
        if (!this.model.isEnabled(index)) {
            if (isSelected) {
                c.setBackground(UIManager.getColor("ComboBox.background"));
            } else {
                c.setBackground(super.getBackground());
            }
            c.setForeground(this.disabledColor);
        } else {
            c.setBackground(super.getBackground());
            c.setForeground(super.getForeground());
        }
        return c;
    }
}
